(function () {
  'use strict';

  angular.module('starter.pages.plan.services', [])
    .factory('Invites', function () {

      var invites = [{
        id: 0,
        name: '林锋',
        phone: '17711367871',
        face: 'img/ben.png',
        status: 1
      }, {
        id: 1,
        name: '林锋',
        phone: '17711367871',
        face: 'img/ben.png',
        status: 1
      }];

      return {
        all: function () {
          return invites;
        },
        remove: function (invite) {
          invites.splice(invites.indexOf(invite), 1);
        },
        get: function (inviteId) {
          for (var i = 0; i < invites.length; i++) {
            if (invites[i].id === parseInt(inviteId)) {
              return invites[i];
            }
          }
          return null;
        },
        getContactByPhone: function (phone) {
          for (var i = 0; i < invites.length; i++) {
            if (invites[i].phone === phone) {
              return invites[i];
            }
          }
          return null;
        }
      };
    })
    .factory('Plans', function () {

      var plans = [{
        id: 0,
        name: '123123123',
        hours: 8,
        status: 0,
        childPlans: [{
          id: 2,
          name: 'test',
          hours: 8,
          status: 0
        }],
        contacts: [{
          id: 0,
          name: '林锋',
          phone: '17711367871',
          face: 'img/ben.png',
          status: 1
        }]
      }, {
        id: 1,
        name: '123123123',
        hours: 8,
        status: 0,
        childPlans: [{
          id: 4,
          name: 'test',
          hours: 8,
          status: 0
        }],
        contacts: [{
          id: 0,
          name: '林锋',
          phone: '17711367871',
          face: 'img/ben.png',
          status: 1
        }]
      }];

      return {
        all: function () {
          return plans;
        },
        remove: function (plan) {
          
          plans.splice(plans.indexOf(plan), 1);
        },
        add: function (plan) {
          var cplan = this.get(plan.id);
          if (!cplan) {
            plans.push(plan);
          } else {
            cplan.name = plan.name;
            cplan.hours = plan.hours;
            cplan.childPlans = plan.childPlans;
          }
        },
        get: function (planId) {
          for (var i = 0; i < plans.length; i++) {
            if (plans[i].id === parseInt(planId)) {
              return plans[i];
            }
          }
          return null;
        }
      };
    })
    .factory('OnePlans', function ($rootScope) {

      var plan = {};

      return {
        next: function () {
          $rootScope.newPlanId -= 1;
          plan = {
            id: $rootScope.newPlanId,
            name: '',
            hours: 8,
            status: 0,
            childPlans: [],
            contacts: [{
              id: 0,
              name: '林锋',
              phone: '17711367871',
              face: 'img/ben.png',
              status: 1
            }]
          };
          return plan;
        },
        set: function (cplan) {
          plan = cplan;
        },
        get: function () {
          return plan;
        },
        addChild: function (cplan) {
          var ccplan = this.getChildByPlanId(cplan.id);
          if (ccplan == null) {
            plan.childPlans.push(cplan);
          } else {
            ccplan.name = cplan.name;
            ccplan.hours = cplan.hours;
            ccplan.content = cplan.content;
            ccplan.status = cplan.status;
          }
        },
        removeChild: function (cplan) {
          var index = this.getChildIndex(cplan.id);
          plan.childPlans.splice(index, 1);
        },
        getChildByPlanId: function (planId) {
          for (var i = 0; i < plan.childPlans.length; i++) {
            if (plan.childPlans[i].id === parseInt(planId)) {
              return plan.childPlans[i];
            }
          }
          return null;
        },
        getChildIndex: function (planId) {
          for (var i = 0; i < plan.childPlans.length; i++) {
            if (plan.childPlans[i].id === parseInt(planId)) {
              return plan.childPlans.indexOf(plan.childPlans[i]);
            }
          }
          return -1;
        },
        getLastChildIndex: function () {
          return plan.childPlans.length - 1;
        },
        addContact: function (contact) {
          var conta = this.getContactByPhone(contact.phone);
          if (!conta) {
            plan.contacts.push(contact);
          } else {
            conta.name = contact.name;
            conta.phone = contact.phone;
            conta.face = contact.face;
          }
        },
        removeContact: function (contact) {
          plan.contacts.splice(plan.contacts.indexOf(contact), 1);
        },
        getContactByPhone: function (phone) {
          for (var i = 0; i < plan.contacts.length; i++) {
            if (plan.contacts[i].phone === phone) {
              return plan.contacts[i];
            }
          }
          return null;
        }
      };
    });

})();
